<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>

                    <el-col :md="12" :sm="12">
                        <el-form-item label="月份:" prop="month.value">
                            <el-date-picker
                                v-model="queryParams.month.value"
                                type="month"
                                placeholder="选择月份">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="站点:" prop="site_id.value">
                            <SelectPage v-model="queryParams.site_id.value" :url="'/admin/site/selectPage'" show_field="site_name" query_field="site_name" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar> -->
            </el-form>

            <el-descriptions title="统计">
                <el-descriptions-item label="现销售额$">{{ztotal_price}}</el-descriptions-item>
                <el-descriptions-item label="现运费￥">{{freightx}}</el-descriptions-item>
                <el-descriptions-item label="现采购￥">{{materialx}}</el-descriptions-item>
                <el-descriptions-item label="物流加成费￥">{{zfreight_addition_w}}</el-descriptions-item>
            </el-descriptions>
            
            <!-- <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row> -->

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="" align="center">
                        <template slot-scope="scope">
                            <!-- <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">订单明细</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->
                            <el-button class="btn-mar" size="mini" type="success" @click="download_sales(scope.row)">下载订单明细</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '统计月份',
                    prop: 'timedate',
                },
                {
                    visible: true,
                    label: '店铺',
                    prop: 'site_name',
                },
                {
                    visible: true,
                    label: '原销售额$',
                    prop: 'total_price',
                },
                {
                    visible: true,
                    label: '物流￥',
                    prop: 'freight',
                },
                {
                    visible: true,
                    label: '采购￥',
                    prop: 'material',
                },
                {
                    visible: true,
                    label: '币种',
                    prop: 'currency',
                },
                // {
                //     visible: true,
                //     label: '广告费',
                //     prop: 'advertisingz',
                // },
                // {
                //     visible: true,
                //     label: '备注',
                //     prop: 'content',
                // },
            ],
            // 接口地址
            api: {
                index: `/admin/sitereport/index`,
                find: `/admin/sitereport/find`,
                add: `/admin/sitereport/add`,
                edit: `/admin/sitereport/edit`,
                delete: `/admin/sitereport/delete`,
                export: `/admin/sitereport/export`,
                status: `/admin/sitereport/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                month: { value: undefined, op: '%like%' },
                site_id: { value: undefined, op: '=' },
            },

        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },
        
        download_sales(row) {  
            const qyparams = {
                id: row.id,
            }
            this.$confirm(`确定要下载订单明细吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {  
                    this.loading = true
                    return this.request.get('/admin/sitereport/download_sales', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response, row.timedate+''+row.site_name+'订单明细.xlsx')
                    this.$message.success('下载成功');
                    this.initIndex();
                })
                .catch(err => {})
                .finally(() => {
                    this.loading = false
                })
        },
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
